package com.tsingtree.modular.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tsingtree.modular.entity.NanGiftSubmission;
import com.tsingtree.modular.mapper.NanGiftSubmissionMapper;
import com.tsingtree.modular.model.params.NanGiftSubmissionParam;
import com.tsingtree.modular.model.result.NanGiftSubmissionResult;
import com.tsingtree.modular.service.NanGiftSubmissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import java.util.Map;
import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tsingtree.base.pojo.page.LayuiPageFactory;
import com.tsingtree.roses.core.util.ToolUtil;
import java.io.Serializable;

/**
 * <p>
 * 精美礼物-提交数据表 服务实现类
 * </p>
 *
 * @author jagger
 * @since 2025-10-24
 */
@Service
public class NanGiftSubmissionServiceImpl extends ServiceImpl<NanGiftSubmissionMapper, NanGiftSubmission> implements NanGiftSubmissionService {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void add(NanGiftSubmissionParam param){
        NanGiftSubmission entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(NanGiftSubmissionParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(NanGiftSubmissionParam param){
        NanGiftSubmission oldEntity = getOldEntity(param);
        NanGiftSubmission newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public NanGiftSubmissionResult findBySpec(NanGiftSubmissionParam param){
        return null;
    }

    @Override
    public List<NanGiftSubmissionResult> findListBySpec(NanGiftSubmissionParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(NanGiftSubmissionParam param){
        Page pageContext = getPageContext();
        IPage<NanGiftSubmissionResult> page = this.baseMapper.customPageList(pageContext, param);
        
        // 解析JSON数据，添加到结果中
        for (NanGiftSubmissionResult result : page.getRecords()) {
            if (ToolUtil.isNotEmpty(result.getFormData())) {
                try {
                    Map<String, Object> formDataMap = objectMapper.readValue(
                        result.getFormData(), 
                        new TypeReference<Map<String, Object>>() {}
                    );
                    result.setFormDataMap(formDataMap);
                } catch (Exception e) {
                    // 如果解析失败，记录日志但不影响其他数据
                    e.printStackTrace();
                }
            }
        }
        
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(NanGiftSubmissionParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private NanGiftSubmission getOldEntity(NanGiftSubmissionParam param) {
        return this.getById(getKey(param));
    }

    private NanGiftSubmission getEntity(NanGiftSubmissionParam param) {
        NanGiftSubmission entity = new NanGiftSubmission();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}

